package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.AddDistributionDto;
import com.huaxiafinance.management.dto.request.UpdateDistributionDto;
import com.huaxiafinance.management.mapper.DistributionRuleMapper;
import com.huaxiafinance.management.mapper.PartnerGradeMapper;
import com.huaxiafinance.management.model.DistributionRule;
import com.huaxiafinance.management.service.DistributionService;
import com.huaxiafinance.management.util.MessageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("distributionService")
public class DistributionServiceImpl implements DistributionService {
    @Autowired
    private DistributionRuleMapper distributionRuleMapper;
    @Autowired
    private PartnerGradeMapper partnerGradeMapper;

    /**
     * 查出分销规则列表
     * @param state
     * @return
     */
    @Override
    public List<Map> getList(Integer state,String name,Integer page,Integer rows) {
        page=page*rows;
        return distributionRuleMapper.getList(state,name,page,rows);
    }

    /**
     * 查出单个规则详情
     * @param id
     * @return
     */
    @Override
    public Map selectOne(Integer id) {

        return distributionRuleMapper.selectOne(id);
    }

    /**
     * 查出等级列表
     * @return
     */
    @Override
    public List<Map> queryList() {

        return partnerGradeMapper.selectIdName();
    }

    /**
     * 新建规则
     * @param dto
     */
    @Override
    public void addOne(AddDistributionDto dto) {
        DistributionRule rule=new DistributionRule();
        BeanUtils.copyProperties(dto,rule);
        //默认不可以
        rule.setIsEnable(0);
        rule.setGmtCreate(new Date());
        distributionRuleMapper.insert(rule);
    }

    /**
     * 编辑规则
     * @param dto
     */
    @Override
    public void updateOne(UpdateDistributionDto dto) {
        DistributionRule rule=distributionRuleMapper.selectByPrimaryKey(dto.getId());
        rule.setOneJoin(getSix(dto.getOneJoin()));
        rule.setOneOperate(getSix(dto.getOneOperate()));
        rule.setTwoJoin(getSix(dto.getTwoJoin()));
        rule.setTwoOperate(getSix(dto.getTwoOperate()));
        rule.setPartnerId(dto.getPartnerId());
        rule.setModifier(dto.getModifier());
        rule.setGmtModified(new Date());
        distributionRuleMapper.updateByPrimaryKey(rule);
    }

    @Override
    public MessageResult updateState(Integer state, Integer id) {
        MessageResult messageResult=new MessageResult();
        DistributionRule rule=distributionRuleMapper.selectByPrimaryKey(id);
        if(state==1){
            if(rule.getIsEnable()==0 && distributionRuleMapper.countGradeIsEnble(rule.getPartnerId())>0){
                messageResult.setFailureResult("同等级存在启用的规则");
                return messageResult;
            }
        }
        rule.setIsEnable(state);
        distributionRuleMapper.updateByPrimaryKeySelective(rule);
        messageResult.setSuccessResult();

        return messageResult;
    }

    /**
     * 获得6位double数
     * @param d
     * @return
     */
    public Double getSix(Double d){
        if(d==null){
            return null;
        }
        d=Math.floor(d*1000000)/1000000;
        return d;
    }

    public Integer countList(Integer state,String name){

        return distributionRuleMapper.countList(state,name);

    }

}
